package com.jianyun.jyzs;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.StrictMode;
import android.support.annotation.RequiresApi;
import android.support.multidex.MultiDex;

import com.amap.api.location.AMapLocation;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.jianyun.jyzs.qrcode.qrcodemanager.QRCodeManager;
import com.jianyun.jyzs.utils.Logger;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.PathUtil;
import com.jianyun.jyzs.utils.PinAlbumBitmapCacheHelper;
import com.jianyun.jyzs.utils.SysConstant2;

import com.jianyun.jyzs.widget.badge.BadgeHandler;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;

import cn.jpush.android.api.JPushInterface;
import io.rong.imkit.utils.SystemUtils;
import okhttp3.OkHttpClient;

/**
 * Created by yangshijun on 2018/5/21.
 */

public class ThisApp extends Application {

    public static volatile Context applicationContext;
    public static volatile Handler applicationHandler;
    public static AMapLocation aMapLocation;
    public static int badgecount = 0;
    public ImageLoader imageLoader;
    private static final String TAG = "JIGUANG-Jyzs";
    private OkHttpClient build;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onCreate() {
        Logger.d(TAG, "[ThisApp] onCreate");
        super.onCreate();

        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush
        rce = this;
        //初始化Fresco
        Fresco.initialize(this);
        //bugly 配置，查看对应崩溃日志。
        String processName = SystemUtils.getCurProcessName(this);
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(this);
        strategy.setUploadProcess(processName.equals(getPackageName()));
        // 初始化Bugly
        CrashReport.initCrashReport(this, "7b5663feae", false, strategy);

        applicationContext = getApplicationContext();
        applicationHandler = new Handler(applicationContext.getMainLooper());
        initImageLoder();


        init7Cecamer();
        //主进程中初始化配置信息。
        if (getApplicationInfo().packageName.equals(SystemUtils.getCurProcessName(this))) {
            if (LoginCache.getInstance().isCacheValid()) {

                new QRCodeManager.Config().setVibrateMode(false).setVoiceMode(false);
                //pin
                PinAlbumBitmapCacheHelper.init(this);
            }
//            企业通讯录配置信息

        }
        BadgeHandler.init(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void init7Cecamer() {
        // android 7.0系统解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

    private void initImageLoder() {
        imageLoader = ImageLoader.getInstance();
        if (SysConstant2.width > 0) {
            if (!imageLoader.isInited()) {
                File cacheDir = new File(PathUtil.imagePathName);
                ImageLoaderConfiguration config = new
                        ImageLoaderConfiguration.Builder(this)
//								.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
//								.memoryCacheExtraOptions(240, 400) // default = device screen dimensions
                        .memoryCacheExtraOptions(Double.valueOf(SysConstant2.width / 2.5).intValue(), Double.valueOf(SysConstant2.height / 3).intValue())
                        .threadPoolSize(3)
                        .threadPriority(Thread.MIN_PRIORITY - 2)
                        .denyCacheImageMultipleSizesInMemory()
                        .discCache(new UnlimitedDiscCache(cacheDir)) // 你可以传入自己的磁盘缓存
                        .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                        .build();
                // Initialize ImageLoader with configuration.
                ImageLoader.getInstance().init(config);
                imageLoader = ImageLoader.getInstance();
            }
        }
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static ThisApp rce;

    public static ThisApp getInstance() {
        return rce;
    }
}
